package com.dgut.smart_house.controller;

import com.dgut.smart_house.domain.Node;
import com.dgut.smart_house.domain.Types;
import com.dgut.smart_house.domain.User;
import com.dgut.smart_house.enums.ResultCode;
import com.dgut.smart_house.service.AdminService;
import com.dgut.smart_house.util.RestResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/admin/api")
public class AdminApiController {

    @Autowired
    private AdminService adminService;

    @RequestMapping("/addNewNode")
    public RestResult addNewNode(Node node) {
        if (adminService.addNewNode(node) != 1) {
            return new RestResult(ResultCode.FAIL.getCode(), "增加新设备类型操作发生错误", null);

        }
        return new RestResult(ResultCode.SUCCESS.getCode(), "增加新设备类型操作成功", null);
    }

    @RequestMapping("/addNewType")
    public RestResult addNewType(Types types) {
        if (adminService.addNewType(types) != 1) {
            return new RestResult(ResultCode.FAIL.getCode(), "增加新数据类型操作发生错误", null);
        }
        return new RestResult(ResultCode.SUCCESS.getCode(), "增加新数据类型操作成功", null);
    }

    @RequestMapping("/findAllUsers")
    public RestResult selectAllUsers() {
        List<User> userList=adminService.selectAllUsers();
        if ( userList== null && userList.size() == 0)
            return new RestResult(ResultCode.FAIL.getCode(), "查询失败", null);
        else
            return new RestResult(ResultCode.SUCCESS.getCode(), "增加新数据类型操作成功", null);
    }

    @GetMapping("/deleteUser")
    public RestResult deleteOneUser(@RequestParam("Id")int Id) {
        int result=adminService.deleteOneUser(Id);
        if ( result== 0)
            return new RestResult(ResultCode.FAIL.getCode(), "删除失败", null);
        else
            return new RestResult(ResultCode.SUCCESS.getCode(), "删除成功", null);
    }


}
